<script setup>
import { ref, onMounted } from 'vue'

import { useExperimentStore } from '@/stores/experimentStore'
import { ArrowDown } from '@element-plus/icons-vue'
import api from '@/api/request'
import { ElMessage } from 'element-plus'

const experimentStore = useExperimentStore()

const experimentData = ref([
  {
    experimentName: '实验1',
    roundName: '第1次实验',
    timePointName: '2024-11-03'
  },
  {
    experimentName: '实验2',
    roundName: '第2次实验',
    timePointName: '2024-11-07'
  }
])

const chartDialogVisible = ref(false)

const imageUrls = ref([]) // 用于存储图片URL

const experimentName = experimentStore.collectedExperiments.experimentName
const teamName = experimentStore.teamName
const data = ref([])

onMounted(async () => {
  try {
    const response = await api.post('/querypicture', {})
    if (response.data.code === 200) {
      data.value = response.data.data
      console.log('data', data.value)
    } else {
      console.error('获取数据失败:', response.data.message)
    }
  } catch (error) {
    console.error('请求失败:', error)
  }
})

const showChart = async () => {
  chartDialogVisible.value = true
  try {
    const picturename = data.value[0]?.picturename

    const response = await api.post('/query', {
      experimentName,
      teamName,
      picture: picturename
    })
    if (response.data.code === 200) {
      imageUrls.value = response.data.data
    } else {
      ElMessage.error('获取图片失败')
    }
  } catch (error) {
    console.error('请求失败:', error)
    ElMessage.error('请求失败')
  }
}

const deleteImage = async () => {
  try {
    const picturename = data.value[0]?.picturename
    const response = await api.post('/deletesacleimage', {
      experimentName,
      values: picturename
    })
    if (response.data.code === 200) {
      ElMessage.success('删除成功')
      imageUrls.value = [] // 清空图片列表
    } else {
      ElMessage.error('删除失败')
    }
  } catch (error) {
    console.error('请求失败:', error)
    ElMessage.error('请求失败')
  }
}
</script>

<template>
  <div>
    <el-header>
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        background-color="lightblue"
        text-color="#fff"
        active-text-color="#ffd04b"
      >
        <el-dropdown>
          <span
            class="history_text"
            style="float: right; font-size: large; color: #101010"
          >
            历史实验
            <el-icon class="el-icon--right">
              <arrow-down />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item
                v-for="experiment in experimentStore.historyExperiments"
                :key="experiment.id"
                @click="selectHistoryExperiment(experiment)"
              >
                {{ experiment.name }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-menu>
    </el-header>
    <el-table :data="experimentData" style="width: 100%">
      <el-table-column prop="experimentName" label="实验名称" width="180" />
      <el-table-column prop="roundName" label="实验次数" width="180" />
      <el-table-column prop="timePointName" label="时间点" width="180" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button @click="showChart(scope.row)">查看图表</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="chartDialogVisible" title="查看图表" width="80%">
      <div v-for="url in imageUrls" :key="url">
        <img
          :src="url"
          style="width: 300px; height: 300px; margin-bottom: 10px"
        />
      </div>

      <template v-slot:footer>
        <el-button @click="deleteImage(url)">删除</el-button>
        <span class="dialog-footer">
          <el-button @click="chartDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
/* 顶部导航栏样式 */
.el-menu-demo {
  border: none;
  background: linear-gradient(135deg, #7367f0 0%, #9f94ff 100%);
  box-shadow: 0 4px 24px rgba(115, 103, 240, 0.08);
  padding: 0 32px;
}

.el-menu-demo .el-menu-item {
  color: #ffffff !important;
  font-weight: 500;
  transition: all 0.3s ease;
  height: 64px;
  line-height: 64px;

  &:hover {
    background-color: rgba(255, 255, 255, 0.1);
    transform: translateY(-2px);
  }
}

.el-menu-demo .history_text {
  color: #ffffff;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 24px;
  height: 64px;
  transition: all 0.3s ease;

  &:hover {
    background: rgba(255, 255, 255, 0.1);
  }
}

.el-menu-demo .history_text .el-icon {
  font-size: 18px;
}

/* 表格样式 */
.el-table {
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 24px rgba(115, 103, 240, 0.08);
  margin: 24px;
}

.el-table th {
  background-color: #f8f8fb !important;
  color: #1e1e2d;
  font-weight: 600;
  padding: 16px 24px;
  border-bottom: 1px solid rgba(115, 103, 240, 0.1);
}

.el-table td {
  padding: 16px 24px;
  color: #6e6b7b;
  border-bottom: 1px solid rgba(115, 103, 240, 0.1);
}

.el-table .el-button {
  padding: 10px 24px;
  border-radius: 8px;
  transition: all 0.3s ease;
  background: linear-gradient(135deg, #7367f0 0%, #9f94ff 100%);
  border: none;
  color: #ffffff;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(115, 103, 240, 0.3);
  }
}

/* 对话框样式 */
.el-dialog {
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 32px rgba(115, 103, 240, 0.15);
}

.el-dialog .el-dialog__header {
  margin: 0;
  padding: 24px;
  border-bottom: 1px solid rgba(115, 103, 240, 0.1);
  background: #ffffff;
}

.el-dialog .el-dialog__header .el-dialog__title {
  font-size: 20px;
  font-weight: 600;
  color: #1e1e2d;
}

.el-dialog .el-dialog__body {
  padding: 32px;
  background: #ffffff;
}

.el-dialog .image-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  padding: 16px;
}

.el-dialog .image-container img {
  width: 100%;
  height: auto;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(115, 103, 240, 0.08);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 32px rgba(115, 103, 240, 0.15);
  }
}

.el-dialog .dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.el-dialog .dialog-footer .el-button {
  padding: 12px 24px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.el-dialog .dialog-footer .el-button--default {
  border: 1px solid #7367f0;
  color: #7367f0;

  &:hover {
    background: rgba(115, 103, 240, 0.1);
    transform: translateY(-2px);
  }
}

.el-dialog .dialog-footer .el-button--primary {
  background: linear-gradient(135deg, #7367f0 0%, #9f94ff 100%);
  border: none;
  color: #ffffff;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(115, 103, 240, 0.3);
  }
}

.el-dialog .dialog-footer .el-button--danger {
  background: linear-gradient(135deg, #ea5455 0%, #f08182 100%);
  border: none;
  color: #ffffff;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(234, 84, 85, 0.3);
  }
}

.page-container {
  padding: 32px;
  background: #ffffff;
  min-height: calc(100vh - 64px);
}

.page-header {
  margin-bottom: 32px;
}

.page-header h1 {
  font-size: 24px;
  font-weight: 600;
  color: #1e1e2d;
  margin: 0 0 16px 0;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    bottom: -8px;
    left: 0;
    width: 40%;
    height: 2px;
    background: linear-gradient(90deg, #7367f0 0%, transparent 100%);
  }
}

.page-header .page-description {
  color: #6e6b7b;
  font-size: 16px;
  margin: 0;
}

.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 300px;
  background: #f8f8fb;
  border-radius: 12px;
  color: #6e6b7b;
}

.image-error .el-icon {
  font-size: 48px;
  margin-bottom: 16px;
  color: #7367f0;
}
</style>
